package testJdk.commonUse.number;

import cn.hutool.core.exceptions.StatefulException;
import org.junit.Test;

/**
 * @version 1.0.0
 * Created by cyd on 2022-02-21 10:11:47
 * @ClassName : learn_jdk.num.TestClassTrans
 * @Description : 类描述
 */
public class TestClassTrans {

    /**
     * long和int的互转
     * int可以直接强转为long
     * long强转为int可能会溢出
     */
    @Test
    public void test01() {
        long count = 0;
        System.out.println((int) count);

        Long size = 10L;
        Integer integer = Integer.valueOf(String.valueOf(size));
        System.out.println(integer);
    }

    /**
     * 测试double和long的类型转换
     */
    @Test
    public void test02() {
        double random = Math.round(Math.random() * 10000);
        System.out.println(random);
        long l = new Double(random).longValue();
        System.out.println(l);
    }

    /**
     * null经过包装类的包装还是null
     */
    @Test
    public void test03() {
        Long count = null;
        System.out.println(count);
    }

    /**
     * 不能转化null，会抛空指针
     */
    @Test
    public void test04() {
        Long aLong = Long.valueOf(null);
        System.out.println(aLong);
    }

    /**
     * 测试转化异常
     */
    @Test
    public void test05() {
        String timestamp = "NaN";
        try {
            Long.parseLong(timestamp.trim());
        } catch (Exception e) {
            throw new StatefulException(400, "Invalid timestamp format: " + e.getMessage());
        }
    }


}
